<template>
  <el-dialog v-model="visible" title="巡检任务管理" width="600px">
    <el-form :model="form" label-width="100px">
      <el-form-item label="任务名称" required>
        <el-input v-model="form.name" placeholder="请输入任务名称" />
      </el-form-item>

      <el-form-item label="执行人员" required>
        <el-select v-model="form.assignees" multiple placeholder="选择执行人员">
          <el-option
            v-for="user in userList"
            :key="user.id"
            :label="user.name"
            :value="user.id"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="截止时间" required>
        <el-date-picker
          v-model="form.dueDate"
          type="datetime"
          placeholder="选择截止时间"
        />
      </el-form-item>

      <el-form-item label="任务说明">
        <el-input
          v-model="form.description"
          type="textarea"
          :rows="4"
          placeholder="请输入详细任务说明"
        />
      </el-form-item>
    </el-form>

    <template #footer>
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="submit">创建任务</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, watch } from 'vue'
import { useProjectStore } from '@/stores/project'

const props = defineProps({
  modelValue: Boolean,
  projectId: Number
})

const emit = defineEmits(['update:modelValue'])

const visible = ref(props.modelValue)
const projectStore = useProjectStore()

const form = ref({
  name: '',
  assignees: [],
  dueDate: '',
  description: ''
})

const userList = ref([
  { id: 1, name: '张三' },
  { id: 2, name: '李四' },
  { id: 3, name: '王五' }
])

watch(
  () => props.modelValue,
  (val) => {
    visible.value = val
  }
)

watch(visible, (val) => {
  emit('update:modelValue', val)
})

const submit = () => {
  projectStore.createInspection(props.projectId, form.value)
  visible.value = false
  form.value = {
    name: '',
    assignees: [],
    dueDate: '',
    description: ''
  }
}
</script>
